#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>

#include "Student.h"

using namespace std;
class StudentRegister
{
private:
	vector<Student> v;
public:

	StudentRegister(){}	
	~StudentRegister(){}

#pragma region Add/List
	void add(Student s)
	{
		v.push_back(s);
	}

	string list()
	{
		ostringstream oss;
		for (auto &s : v)
		{
			oss << s << endl;
			oss << s.printCourses();
		}
		return oss.str();
	}
#pragma endregion

#pragma region Files
	//Data format object=(*data*)
	//Student=(Kalle,Anka,1991)
	//Course=(courseName,Year)
	//Result=(testName,grade)
	//Result=(testName,grade)
	void WriteToFile(string fileName)
	{
		ofstream fout;
		try
		{
			fout.open(fileName);
			for (auto &s : v)
			{
				fout << "Student=" << s << endl;
				fout << s.printCourses();
			}
		}
		catch (exception e)
		{
			cout << "Exception while writing to file: " << fileName << endl;
			cout << e.what();
		}
		fout.close();
	}

	void ReadFromFile(string fileName)
	{
		v.clear(); //clear any previous students in register
		string str, row;
		ifstream fin;
		try
		{
			fin.open(fileName);
			while (getline(fin, str))
			{
				istringstream iss(str);
				getline(iss, str, '=');
				if (str.compare("Student") == 0)
				{
					Student s;
					iss >> s;
					if (s.getEnrollmentYear() != -1)
					{
						v.push_back(s);
					}
				}
				else if (str.compare("Course") == 0)
				{
					Course c;
					iss >> c;
					if (c.getYear() != -1)
					{
						addCourseToLatest(c);
					}
				}
				else if (str.compare("Result") == 0)
				{
					Result r;
					iss >> r;
					if (r.getName().compare("") != 0)
					{
						addResultToLatest(r);
					}
				}
			}
		}
		catch (exception e)
		{
			cout << "Exception while reading from file: " << fileName << endl;
			cout << e.what();
		}
		fin.close();
	}
#pragma endregion

	void addCourseToLatest(Course c)
	{
		v[v.size() - 1].addCourse(c);
	}

	void addResultToLatest(Result r)
	{
		v[v.size() - 1].addResultToLatest(r);
	}

	void listStudents()
	{
		for (int i = 0; i < v.size(); i++)
		{
			cout << "id " << i << ": " << v[i] << endl;
		}
	}
	
	void removeStudent(int i)
	{
		v.erase(v.begin() + i);
	}


	void menu()
	{
		bool runMenu = true;
		while (runMenu)
		{
			int i;
			system("cls");
			cout << endl;
			cout << "1: Add Student" << endl;
			cout << "2: Remove Student" << endl;
			cout << "3: Add Course For Student" << endl;
			cout << "4: Add Result For Student" << endl;
			cout << "5: List Register" << endl;
			cout << "6: Save Register To File" << endl;
			cout << "7: Load Register From File" << endl;
			//cout << "8." << endl;
			//cout << "9." << endl;
			cout << "0: Exit" << endl;
			cout << endl;
			cin >> i;
			cin.get(); //remove trailing '\n'
			system("cls");

			//helper variables
			int i1, i2;
			Student s;
			string str;
			Course c;
			Result r;

			switch (i)
			{
			case 1: //Add Student
				s.read();
				v.push_back(s);
				break;
			case 2: //Remove Student
				listStudents();
				cout << "Remove student with id: ";
				cin >> i1;
				cin.get(); //remove trailing '\n' 
				removeStudent(i1);
				break;
			case 3: //Add Course For Student
				listStudents();
				cout << "Add course for student with id: ";
				cin >> i1;
				cin.get(); //remove trailing '\n'
				c.read();
				v[i1].addCourse(c);
				break;
			case 4: //Add Result For Student
				listStudents();
				cout << "Add result for student with id: ";
				cin >> i1;
				cin.get(); //remove trailing '\n'
				v[i1].listCourses();
				cout << "For course with id: ";
				cin >> i2;
				cin.get(); //remove trailing '\n'
				r.read();
				v[i1].addResult(i2, r);								
				break;
			case 5: //List Register
				cout << list();
				system("pause");
				break;
			case 6: //Save Register To File
				cout << "File to save data to: ";
				getline(cin, str);
				WriteToFile(str);
				break;
			case 7: //Load Register From File
				cout << "File to load data from: ";
				getline(cin, str);
				ReadFromFile(str);
				break;
			//case 8:
			//	break;
			//case 9:
			//	break;
			default:
				runMenu = false;
				break;
			}//end-switch
		}
	}
};

